﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Eudaemon.ProeAssist.Core
{
    /// <summary>
    /// Call IsAllFieldsValid() to check if the object is valid
    /// </summary>
    public class ProeFileBundle
    {
        public ProeFileBundle(string fileName, ProeFileType fileType, int initVersion)
        {
            this.FileName = fileName;
            this.FileType = fileType;
            _versions = new List<int>();
            this.Add(initVersion);
        }

        /// <returns>False if failed to add</returns>
        public ProeFileBundle(string fileFullName)
        {
            if (!ProeFileType.IsProeFileName(fileFullName)) 
                FailedToInitialize();
            string[] splits = fileFullName.Split(new Char[] { '.' });
            // 1. name; 2. file type(enumeration; 3. version number(positive integer).
            this.FileName = splits[0];
            switch (splits[1].ToLower())
            {
                case "asm":
                    _fileType = new ProeAssemblyType();
                    break;
                case "dgm":
                    _fileType = new ProeDiagramType();
                    break;
                case "drw":
                    _fileType = new ProeDrawingType();
                    break;
                case "frm":
                    _fileType = new ProeFormatType();
                    break;
                case "lay":
                    _fileType = new ProeLayoutType();
                    break;
                case "mfg":
                    _fileType = new ProeManufacturingType();
                    break;
                case "mrk":
                    _fileType = new ProeMarkupType();
                    break;
                case "prt":
                    _fileType = new ProePartType();
                    break;
                case "rep":
                    _fileType = new ProeReportType();
                    break;
                case "sec":
                    _fileType = new ProeSketchType();
                    break;
                default:
                    FailedToInitialize();
                    break;
            }

            int version = Int32.Parse(splits[2]);
            _versions = new List<int>();
            this.Add(version);
        }

        public string FileName
        {
            get
            {
                return _fileName;
            }
            set
            {
                _fileName = value;
            }
        }

        public string FileNameWithExtension
        {
            get
            {
                return _fileName + "." + _fileType.Abbreviation;
            }
        }

        public ProeFileType FileType
         {
            get
            {
                return _fileType;
            }
            set
            {
                 _fileType = value;
            }
        }

        public int[] GetAllVersions()
        {
            return _versions.ToArray();
        }

        //public string[] GetAllFiles()
        //{
        //    int[] versions = ShowAllVersions();
        //    string[] files = new string[versions.Length];
        //    string fileName = FileName + "." + FileType.Abbreviation + ".";
        //    for (int i = 0; i < files.Length; ++i)
        //    {
        //        files[i] = fileName + versions[i].ToString();
        //    }
        //    return files;
        //}

        public int NewestVersion
        {
            get
            {
                _versions.Sort();
                int index = _versions.Count - 1;
                if (index >= 0)
                    return _versions[index];
                else
                    return -1;
            }
        }

        public int VersionsCount()
        {
            return _versions.Count;
        }

        public void Add(int version)
        {
            if (version<0)
                return;
            if (IsVersionExist(version))
                return;
            _versions.Add(version);
        }

        public void Remove(int version)
        {
            _versions.Remove(version);
        }

        public bool IsVersionExist(int version)
        {
            return _versions.Contains(version);
        }

        public override string ToString()
        {
            return this.FileName + "." + this.FileType.Abbreviation;
        }

        public override bool Equals(object obj)
        {
            if (obj != null && obj is ProeFileBundle)
            {
                if (obj.ToString() == this.ToString())
                    return true;
                else
                    return false;
            }
            return false;
        }

        public override int GetHashCode()
        {
            return ToString().GetHashCode();
        }

        private void FailedToInitialize()
        {
            throw new ApplicationException(@"Failed to create the 'ProeFileBundle' object.");
        }

        #region Protected & Private
        private string _fileName;
        private ProeFileType _fileType;
        private List<int> _versions;
        #endregion
    }
}
